package dyyx;

import java.util.Random;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

public class BloomFilterTest {
	
	private static final Random rand = new Random();
	
	public static void main(String[] args) throws Exception {
		
		// BloomFilter bloomFilter = new BloomFilter();
		// Funnels.stringFunnel(Charset.forName("utf-8"))
		
		int expectedInsertions = 1000000;
		double ffp = 0.1 / 100 ;
		// ffp = 0.1;
		// int expectedInsertions
		// fpp the desired false positive probability
		BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), expectedInsertions, ffp);
        
		
		// long aec = bloomFilter.approximateElementCount();	
		// System.out.println("approximateElementCount="+aec);
		
	
		for(int i=0;i<expectedInsertions;i++){
			bloomFilter.put(i);
		}
		// aec = bloomFilter.approximateElementCount();
		// System.out.println("approximateElementCount="+aec);
		
		int trueNum = 0;
		int falseNum = 0;
        boolean flag;
		for(int i=0;i<expectedInsertions;i++){
			flag = bloomFilter.mightContain(i);
			if(flag){
				trueNum ++;
			}else{
				falseNum ++ ;
			}
		}
		
		System.out.println("falseNum="+falseNum+",trueNum="+trueNum);
		
		// 一定不存在
		trueNum = 0;
		falseNum = 0;
		for(int i=expectedInsertions;i< expectedInsertions*2;i++){
			flag = bloomFilter.mightContain(i);
			if(flag){
				trueNum ++;
			}else{
				falseNum ++ ;
			}
		}
		
		System.out.println("falseNum="+falseNum+",trueNum="+trueNum);
		
		System.out.println(falseNum * 1.0 / expectedInsertions);
		
		bloomFilter.mightContain(9);

	}
	
}
